﻿using System;
using Abp.AspNetCore;
using Abp.AspNetCore.Configuration;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Timing;
using AbpDemo.Configuration;
using AbpDemo.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace AbpDemo.WebApi
{
    [DependsOn(
        typeof(AbpDemoApplicationModule),
        typeof(AbpDemoEntityFrameworkCoreModule),
        typeof(AbpAspNetCoreModule))]
    public class AbpDemoWebApiModule : AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;

        public AbpDemoWebApiModule(IHostingEnvironment env)
        {
            _appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName);
        }

        public override void PreInitialize()
        {
            Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(AbpDemoConsts.ConnectionStringName);
            //TODO: timing多时区时间
            Clock.Provider = ClockProviders.Utc;
            //Configuration.Navigation.Providers.Add<AbpDemoNavigationProvider>();
            Configuration.Modules.AbpAspNetCore()
                .CreateControllersForAppServices(
                    typeof(AbpDemoApplicationModule).GetAssembly()
                );
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(AbpDemoWebApiModule).GetAssembly());
        }
    }
}
